# 用户注册

def register():
    username = input("输入用户名：")
    password = input("输入密码：")
    re_password = input("输入确认密码：")
    if password == re_password:
        # 保存信息 ，a  是追加
        with open("users.txt", 'a') as w_stream:
            w_stream.write('{} {}\n'.format(username, password))

        print("用户注册成功")
    else:
        print("密码不一致！")


# 用户登录

def login():
    username = input("输入用户名：")
    password = input("输入密码：")
    if username and password:
        with open("users.txt", 'r') as r_stream:
            while r_stream.readable():
                line = r_stream.readline().replace("\n", "").split(" ")
                name = line[0]
                pwd = line[1]
                if username == name and password == pwd:
                    print("登录成功！")
                    break
    else:
        print("用户名或者密码错误！")


# 查看图书
def show_book():
    print("欢迎进入图书管理系统".center(40, "*"))
    with open("books.txt", mode='r', encoding="utf8") as r_stream:
        lines = r_stream.readlines()
        for line in lines:
            print(line, end="")
            return line


# 是否存在
def is_exist(book_name):
    with open("books.txt", mode='r', encoding="utf8") as r_stream:
        lines = r_stream.readlines()
        for line in lines:
            if book_name + "\n" == line:
                return True
        else:
            return False


# 用户和图书的关系
def borrow_books(username):
    line = "{}：".format(username)
    while True:
        book_name = input("输入需要借书的名称：")
        if is_exist(book_name):
            line += book_name + "->"
            flag = input("是否继续（1：继续，2：退出）：")
            if flag == "2":
                print("借书完成！")
                break
    with open("user_book.txt", 'a', encoding="utf8") as w_stream:

        w_stream.write(line[: line.rindex("->")] + "\n")


# 注册
# register()
# 登录
# login()
# 查看图书
# show_book()


borrow_books("sanduo")
